package indi.zhifa.engine.core.common.config;

import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;

public class FastjsonConfig {

    /**
     *  标准的fastJson设置
     */
    public static FastJsonConfig getDefaultFastJsonConfig(){
        FastJsonConfig config = new FastJsonConfig();
        config.setWriterFeatures(
                // 保留 Map 空的字段
                JSONWriter.Feature.WriteMapNullValue,
                // 将 String 类型的 null 转成""
                JSONWriter.Feature.WriteNullStringAsEmpty,
                // 将 List 类型的 null 转成 []
                JSONWriter.Feature.WriteNullListAsEmpty,
                // 将 Boolean 类型的 null 转成 false
                // BigDecimal不使用科学计数法
                JSONWriter.Feature.WriteBigDecimalAsPlain,
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                // 漂亮的输出，这里是学习用，正式工程请注释掉
                JSONWriter.Feature.PrettyFormat,
                // 把Long转化为String
                JSONWriter.Feature.WriteLongAsString,
                // 把BigDecimal转化为String
                JSONWriter.Feature.BrowserCompatible,
                // 枚举
                JSONWriter.Feature.WriteEnumsUsingName);
        config.setReaderFeatures(
                JSONReader.Feature.Base64StringAsByteArray
        );
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return config;
    }

    /**
     * 输出简单的fastjson
     *
     */
    public static FastJsonConfig getSimpleFastJsonJsonConfig(){
        FastJsonConfig config = new FastJsonConfig();
        config.setWriterFeatures(
                JSONWriter.Feature.WriteBigDecimalAsPlain,
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                // 漂亮的输出，这里是学习用，正式工程请注释掉
                //JSONWriter.Feature.PrettyFormat,
                // 把Long转化为String
                JSONWriter.Feature.WriteLongAsString,
                // 把BigDecimal转化为String
                JSONWriter.Feature.BrowserCompatible,
                // 枚举
                JSONWriter.Feature.WriteEnumsUsingName,
                // 把byteArray改成base64
                JSONWriter.Feature.WriteByteArrayAsBase64);
        config.setReaderFeatures(
                JSONReader.Feature.Base64StringAsByteArray
        );
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return config;
    }

    /**
     * 需要序列化的fastjson，变量携带类型
     *
     * @return
     */
    public static FastJsonConfig getSerializeFastJsonConfig(){
        FastJsonConfig config = new FastJsonConfig();
        config.setWriterFeatures(
                JSONWriter.Feature.WriteNullListAsEmpty,
                // 写入类名
                JSONWriter.Feature.WriteClassName,
                // 将 Boolean 类型的 null 转成 false
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                JSONWriter.Feature.WriteEnumsUsingName);
        config.setReaderFeatures(
                JSONReader.Feature.SupportClassForName,
                // 支持autoType
                JSONReader.Feature.SupportAutoType);

        return config;
    }


}
